<template>
  <div class="art-pub-container">
    <el-form
      :model="pubForm"
      :rules="pubRules"
      ref="pubRef"
      label-width="100px"
    >
      <!-- title -->
      <el-form-item label="文章标题" prop="title" placeholder="请填写文章标题">
        <el-input v-model="pubForm.title" maxlength="30"></el-input>
      </el-form-item>
      <!-- 下拉框 -->
      <el-form-item label="文章分类" prop="cate_id">
        <el-select v-model="pubForm.cate_id" placeholder="请选择分类" clearable>
          <el-option
            v-for="item in artCateList"
            :key="item.id"
            :label="item.cate_name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!--  richText----content--TODO -->
      <el-form-item label="文章内容">
        <!-- pubForm.content -->
        <quill-editor v-model="pubForm.content"> </quill-editor>
      </el-form-item>

      <!-- 封面图片 -->
      <el-form-item label="文章封面">
        <div class="cover-box">
          <!-- pubForm.cover_img => import 导入的默认图片 -->
          <img :src="pubForm.cover_img" alt="" class="cover-img" />
          <!-- 按钮的功能TODO -->
          <!-- <input type="file" style="display:none"> -->
          <input type="file" hidden ref="file" @change="getFile" />
          <el-link type="primary" :underline="false" @click="choseImage"
            >+ 选择封面</el-link
          >
        </div>
      </el-form-item>
      <el-form-item>
        <!-- TODO  submit btn -->
        <el-button @click="publish('已发布')" type="primary"
          >发布文章</el-button
        >
        <el-button @click="publish('草稿')" type="info">存为草稿</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// import '@/assets/images/a.css'

// import a from '@/assets/images/cover.jpg'
// img.src = a

import defaultCover from '@/assets/images/cover.jpg'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { mapState, mapActions } from 'vuex'
import { pubArticle } from '@/api/article.js'
// console.log(defaultCover) // defaultCover 图片路径

export default {
  name: 'ArtPub',
  components: {
    quillEditor
  },
  data () {
    return {
      // 发布文章的表单
      pubForm: {
        title: '', // 文章标题
        cate_id: '', // 文章分类id
        content: '', // 文章内容
        state: '', // 文章发布状态
        cover_img: defaultCover // 文章封面图
      },
      //   表单校验规则
      // blur min max message
      pubRules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
          {
            min: 1,
            max: 30,
            message: '文章标题的长度为1-30个字符',
            trigger: 'blur'
          }
        ],
        cate_id: [
          { required: true, message: '请选择文章分类', trigger: 'blur' }
        ]
      },
      file: {}
    }
  },
  created () {
    //   获取文章分类
    this.getArtCateList()
  },
  computed: {
    // artCateList = []
    ...mapState('article', ['artCateList'])
  },
  methods: {
    // getArtCateList(){}
    ...mapActions('article', ['getArtCateList']),
    // 发布文章
    async publish (state) {
      // 收集表单数据this.pubForm
      this.pubForm.state = state
      const fm = {
        title: this.pubForm.title,
        cate_id: this.pubForm.cate_id,
        content: this.pubForm.content,
        state: this.pubForm.state,
        cover_img: this.file
      }
      // console.log(fm) // 二进制
      // 请求=>api和响应
      const {
        data: { code, message }
      } = await pubArticle(fm)
      // console.log(code, message)
      if (code !== 0) return this.$message.error(message)
      this.$message.success(message)

      this.$emit('closeDiaPub')
    },
    // @change="" 获取本地图片
    getFile (e) {
      const file = e.target.files[0]
      // console.log(file)
      if (file) {
        // 预览
        // this.pubForm.cover_img = file ?能不能写? => 不能 => file => 路径
        // img的src => pubForm.cover_img => 显示?
        // img的src = "网址图片 || 图片路径 || base64 "
        // TODO
        const tmp = URL.createObjectURL(file)
        // console.log(tmp) // blob => 二进制数据
        this.pubForm.cover_img = tmp
        // this.file = tmp
        this.file = file
      }
    },
    // 选择本地图片
    choseImage () {
      // 触发type="file"的click()方法 => 获取input => ref操作dom
      // this.$refs.file.value
      this.$refs.file.click()
    }
  }
}
</script>

<style lang="less" scoped>
.art-pub-container {
  padding: 0 30px 20px 0px;
}

.el-select {
  width: 100%;
}

/deep/ .ql-snow .ql-color-picker .ql-picker-label svg,
/deep/ .ql-snow .ql-icon-picker .ql-picker-label svg {
  position: relative;
  top: -6px;
}

/deep/ .ql-snow .ql-picker-label::before {
  position: relative;
  top: -8px;
}

/deep/ .ql-editor {
  min-height: 300px;
}

.cover-img {
  object-fit: cover;
  width: 400px;
  height: 280px;
  margin-right: 10px;
}

.cover-box {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}
</style>

// SSR => 服务端渲染 => data + 页面 => 返回 => 显示
